
const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    @property(cc.Button)
    private closeBtn:cc.Button = null;

    @property(cc.PageView)
    private pageView: cc.PageView = null;

    @property(cc.Sprite)
    private leftBtn:cc.Sprite = null;

    @property(cc.Sprite)
    private rightBtn:cc.Sprite = null;

    @property(cc.Prefab)
    private pagItem: cc.Prefab = null;

    @property(cc.Label)
    private pageLabel: cc.Label = null;

    // onLoad () {}

    start () {
        this.closeBtn.node.on(cc.Node.EventType.TOUCH_END,this.clean,this);
        this.leftBtn.node.on(cc.Node.EventType.TOUCH_END,this.onLeftClick,this);
        this.rightBtn.node.on(cc.Node.EventType.TOUCH_END,this.onRightClick,this);
        this.initUI();
    }

    private initUI(){
        for (let i = 0; i < 12; i++) {
            let node = cc.instantiate(this.pagItem);
            if(node)
            {   
                cc.loader.loadRes(`img/m_weapon_${i+1}`, cc.SpriteFrame, (err, spriteFrame)=>{
                    node.getChildByName("wuqiIcon").getComponent(cc.Sprite).spriteFrame = spriteFrame;
                });
               
                if(i!=this.pageView.getCurrentPageIndex())
                {
                    node.setScale(0.7);
                }
                this.pageView.addPage(node);
            }
        }

        this.curIndex = this.pageView.getCurrentPageIndex();
        this.pageView.scrollToPage(this.curIndex, 0);
        this.updateUI(this.curIndex);
    }

    // update (dt) {}

    private curIndex = 0;
    private onLeftClick() {
        this.curIndex--;
        if (this.curIndex < 0) {

            this.curIndex = 0;
            cc.warn("已经到第一页了")
            return;
        }
        //this.rolePage.setCurrentPageIndex(this.curIndex);
        this.pageView.scrollToPage(this.curIndex, 1);
        // this.leftClickCount++;
        // //显示大小变化
        // this.itemScale(this.leftClickCount);
        console.log(this.curIndex);
        this.itemScale(this.curIndex);
        this.updateUI(this.curIndex);

    }

    private onRightClick() {
        this.curIndex++;
        if (this.curIndex > this.pageView.getPages().length - 1) {
            this.curIndex = this.pageView.getPages().length - 1;
            cc.warn("已经到最后一页了")
            return;
        }
        this.pageView.scrollToPage(this.curIndex, 1);//滚动到指定页面   页号  滚动时间
        console.log(this.curIndex);
        this.itemScale(this.curIndex);
        this.updateUI(this.curIndex);
    }

     //Item节点变化 
     private itemScale(clickCount) {
        //     console.log("当前选择页面index:", this.rolePage.getCurrentPageIndex());
        const pageChilds = this.pageView.getPages();//获取所有的子页面
        for (clickCount = 0; clickCount < pageChilds.length; clickCount++) {
            if (clickCount != this.pageView.getCurrentPageIndex()) {
                let action = cc.scaleTo(0.2, 0.7, 0.7)
                pageChilds[clickCount].runAction(action);
                //node.runAction(action.repeatForever()); // right 一直执行
            } else {
                let action = cc.scaleTo(0.2, 1, 1)
                pageChilds[clickCount].runAction(action);

            }
        }
    }

    private updateUI(curPageIndex:number){
        this.pageLabel.string = `${curPageIndex + 1}/12`;
    }


    private clean():void
    {   
        this.closeBtn.node.off(cc.Node.EventType.TOUCH_END,this.clean,this); 
        this.node.destroy();
    }
}
